
#include "voice_common.h"
#include "gkt_dac.h"

//warn: You can't add printing to the function unless necessary
int voice_output_decode_blocks(voice_output_s *output)
{
	uint32_t size = (output->total_frame_nums-output->total_left_frame_nums) * GKT_VOICE_DEC_INPUT_FRAME_SIZE;
	uint8_t *block_buffer = output->dec_buffer;
	uint32_t frame_nums;
	const uint32_t dec_one_frame_bytes = VOICE_OUTPUT_DEC_FRAME_BYTES*GKT_VOICE_DEC_CONVERSION_SAMPLE_MULTIPLE;
	int temp_size;

	gkt_trace("decode_blocks: (meta_left_frame_nums - size_freme)(%u - %u)\n",
			output->total_left_frame_nums, size);

	frame_nums = VOICE_OUTPUT_DEC_BLOCK_FRAMES;
	if (output->total_left_frame_nums == output->total_frame_nums) {
		gkt_voice_decode(output->volume, 1, output->meta_buffer, block_buffer, GKT_FRAME_TYPE_HEAD);
		output->total_left_frame_nums -= 1;
		frame_nums -= 1;
		size = GKT_VOICE_DEC_INPUT_FRAME_SIZE;
		block_buffer += (dec_one_frame_bytes);
		if (frame_nums >= output->total_left_frame_nums) {
			if (frame_nums == output->total_left_frame_nums) {
				gkt_delay_ms(14);
				gkt_voice_decode(output->volume, frame_nums, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_TAIL);
				output->total_left_frame_nums = 0;
				return GKT_SUCCESS;
			}
			else {
				frame_nums = output->total_left_frame_nums;
				gkt_voice_decode(output->volume, frame_nums, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_TAIL);
				block_buffer += (frame_nums*dec_one_frame_bytes);
				temp_size = (VOICE_OUTPUT_DEC_BLOCK_FRAMES-1-frame_nums) * dec_one_frame_bytes;
				memset(block_buffer, 0x00, temp_size);//Fill mute frame
				block_buffer += temp_size;
				//14ms is the approximate playing time of 0xF0-byte voice data,
				//and the setting of DMA is prohibited before the playback is completed
				gkt_delay_ms(14);

				memset(block_buffer, 0x00, dec_one_frame_bytes);//Fill mute frame
				//gkt_dac_reset_dma(block_buffer, (frame_nums+1) * VOICE_OUTPUT_DEC_FRAME_BYTES * GKT_VOICE_DEC_CONVERSION_SAMPLE_MULTIPLE);
				output->total_left_frame_nums = 0;
				return GKT_SUCCESS;
			}
		}
	}
	else if (output->total_left_frame_nums == 0) {
		return GKT_SUCCESS;
	}
	else if (output->total_left_frame_nums == VOICE_OUTPUT_DEC_BLOCK_FRAMES) {
		frame_nums = output->total_left_frame_nums-1;
		gkt_voice_decode(output->volume, frame_nums, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_MIDDLE);
		block_buffer += (frame_nums*dec_one_frame_bytes);
		size = (frame_nums*GKT_VOICE_DEC_INPUT_FRAME_SIZE);
		gkt_delay_ms(14);
		gkt_voice_decode(output->volume, 1, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_TAIL);
		output->total_left_frame_nums = 0;
		return GKT_SUCCESS;
	}
	else if (output->total_left_frame_nums < VOICE_OUTPUT_DEC_BLOCK_FRAMES){
		frame_nums = output->total_left_frame_nums;
		gkt_voice_decode(output->volume, frame_nums, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_TAIL);
		block_buffer += (frame_nums*dec_one_frame_bytes);
		temp_size = (VOICE_OUTPUT_DEC_BLOCK_FRAMES-frame_nums-1) * dec_one_frame_bytes;
		memset(block_buffer, 0x00, temp_size);//Fill mute frame
		block_buffer += temp_size;

		gkt_delay_ms(14);
		memset(block_buffer, 0x00, dec_one_frame_bytes);
		output->total_left_frame_nums = 0;
		return GKT_SUCCESS;
	}

	frame_nums = frame_nums - 1;
	gkt_voice_decode(output->volume, frame_nums, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_MIDDLE);
	output->total_left_frame_nums -= (frame_nums);
	block_buffer += (frame_nums*dec_one_frame_bytes);

	//Prevent decoding from being too fast and flushing out the threshold data that has not yet been played
	gkt_delay_ms(14);
	size = (output->total_frame_nums-output->total_left_frame_nums) * GKT_VOICE_DEC_INPUT_FRAME_SIZE;
	gkt_voice_decode(output->volume, 1, &output->meta_buffer[size], block_buffer, GKT_FRAME_TYPE_MIDDLE);
	output->total_left_frame_nums -= 1;

	return GKT_SUCCESS;

}
